package com.yanyeori.framework.datasource.config;

import com.yanyeori.framework.core.util.ThreadLocalUtil;

/**
 * 数据源切换
 *
 * @author chenkuan 2020/8/6
 */
public class DatabaseContextHolder {
    /**
     * ThreadLocal：存在于每个线程Thread的ThreadLocalMap中，ThreadLocalMap的key为当前ThreadLocal对象，value为值
     */
    private static final ThreadLocal<String> CONTEXT_HOLDER = ThreadLocalUtil.createTransmittable();

    public static void setDBKey(String dbKey) {
        CONTEXT_HOLDER.set(dbKey);
    }

    public static String getDBKey() {
        return CONTEXT_HOLDER.get();
    }

    public static void clearDBKey() {
        CONTEXT_HOLDER.remove();
    }
}
